﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    public class CurrencyMap : EntityTypeConfiguration<Currency> {
        public CurrencyMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.com_currencies");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(c => c.CurrencyId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            Property(c => c.Code)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_COM_CUR_CD", 1) { IsUnique = true }));

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // 0..1:n Country
            HasMany<Country>(c => c.Countries)
                .WithOptional(c => c.Currency)
                .HasForeignKey(c => c.CurrencyId);

            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(c => c.CurrencyId)
                .HasColumnName("cur_id");

            Property(c => c.Code)
                .IsRequired()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnName("cur_cd");

            Property(c => c.Name)
                .IsOptional()
                .IsUnicode(false)
                .HasMaxLength(250)
                .HasColumnName("cur_desc");

            Property(c => c.MinorUnit)
                .IsOptional()
                .HasColumnName("cur_minor_unit");

            Property(c => c.NumericCode)
                .IsOptional()
                .HasColumnName("cur_numeric_cd");
        }
    }
}
